package admin.core.utils;

public class TypeUtils {
    public static Object convertStringType(String text, Class<?> type) {
        if (type == Integer.TYPE || type == Integer.class) {
            return Integer.parseInt(text);
        } else if (type == Long.TYPE || type == Long.class) {
            return Long.parseLong(text);
        } else if (type == Float.TYPE || type == Float.class) {
            return Float.parseFloat(text);
        } else if (type == Double.TYPE || type == Double.class) {
            return Double.parseDouble(text);
        } else if (type == Boolean.TYPE || type == Boolean.class) {
            return Boolean.parseBoolean(text);
        } else {
            return text; // 默认为字符串类型
        }
    }

    public static Object convertIdType(Object idValue, Class<?> idFieldType) {
        Object id = null;
        if (idFieldType == Integer.class || idFieldType == int.class) {
            id = Integer.valueOf(idValue.toString());
        } else if (idFieldType == Long.class || idFieldType == long.class) {
            id = Long.valueOf(idValue.toString());
        } else if (idFieldType == String.class) {
            id = idValue.toString();
        } else {
            throw new IllegalArgumentException("Unsupported ID field type: " + idFieldType);
        }
        return id;
    }
}
